/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper;

import com.openjawtech.xscraper.scraper.util.FileUtil;
import com.openjawtech.xscraper.scraper.util.ClassLoaderUtil;
import com.openjawtech.xscraper.scraper.util.PropertiesUtil;
import com.openjawtech.xscraper.scraper.exception.ConfigurationException;
import com.openjawtech.xscraper.flights.dao.impl.HibernateSessionFactory;
import java.io.File;
import java.io.IOException;
import com.openjawtech.xscraper.scraper.filter.JarFileFilter;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @Created on 24-Apr-2009, 01:24:46
 * @author Wenbo.Sun
 */
public class StartupConfiguration {

    public static final String CLASSESFOLDER = "classes";
    public static final String LIBFOLDER = "lib";
    public static final String CONFIGFOLDER = "config";
    public static final String SAVEDFOLDER = "save";
    public static final String HIBERNATEFILE = "hibernate.properties";
    public static final String CONFIGPROPERTIESFILE = CONFIGFOLDER + "/config.properties";
    private static final Log log = LogFactory.getLog(StartupConfiguration.class);

    public void init() {
        initFolders();
        loadClasses(CLASSESFOLDER);
        loadLib(LIBFOLDER);
        addHibernatProperties();
    }

    public void initFolders() {
        log.info("initial folders ...");
        try {
            if (!FileUtil.isFolderAndExisted(CLASSESFOLDER)) {
                FileUtil.makeDir(".", CLASSESFOLDER);
            }
            if (!FileUtil.isFolderAndExisted(LIBFOLDER)) {
                FileUtil.makeDir(".", LIBFOLDER);
            }
            if (!FileUtil.isFolderAndExisted(CONFIGFOLDER)) {
                FileUtil.makeDir(".", CONFIGFOLDER);
            }
            if (!FileUtil.isFolderAndExisted(SAVEDFOLDER)) {
                FileUtil.makeDir(".", SAVEDFOLDER);
            }
        } catch (Throwable t) {
            log.error(new ConfigurationException("initial folders failed !", t));
        }
    }

    public void loadClasses(String classes) {
        log.info("loading classes ...");
        try {
            ClassLoaderUtil.addFile(classes);
        } catch (IOException ex) {
            log.error(new ConfigurationException("load classes failed !", ex));
        }
    }

    public void loadLib(String lib) {
        log.info("loading lib ...");
        try {
            File file = new File(lib);
            for (File jar : file.listFiles(new JarFileFilter())) {
                ClassLoaderUtil.addFile(jar);
            }
        } catch (IOException ex) {
            log.error(new ConfigurationException("load lib failed !", ex));
        }
    }

    public Properties getConfigProperties() {
        Properties configProperties = null;
        try {
            log.info("retriving configuration properties ...");
            configProperties = PropertiesUtil.getProperties(CONFIGPROPERTIESFILE);
        } catch (Throwable t) {
            log.error("retireve configuration properties failed", t);
        }
        return configProperties;
    }

    public void addHibernatProperties() {
        Properties pros = PropertiesUtil.getProperties(CONFIGFOLDER + "/" + HIBERNATEFILE);
        HibernateSessionFactory.addProperties(pros);
    }
}
